package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.medical.S201;
import com.gxuwz.medical.domain.money.Money;
import com.gxuwz.medical.domain.vo.Page;
import com.gxuwz.medical.exception.DbException;

public class FamilyDao extends GenericDao<Family> {
	
	@Override
	protected Family handle(ResultSet rs) throws SQLException {
		try{
			Family family = new Family();
			family.setFamilycode(rs.getString("familycode"));
			family.setAreacode(rs.getString("areacode"));
			family.setFamilyattribute(rs.getString("familyattribute"));
			family.setPopulation(rs.getString("population"));
			family.setFarmpopulation(rs.getString("farmpopulation"));
			family.setFamilyaddress(rs.getString("familyaddress"));
			family.setCreattime(rs.getTimestamp("creattime"));
			family.setRegistrar(rs.getString("registrar"));
			family.setFamilyholdername(rs.getString("familyholdername"));
			return family;
		}catch(SQLException e){
			
			throw new SQLException("结果集转为实例失败!",e);
		}
	}
	
	public List<Family>findAll()throws DbException{
		try{
		  String sql="select * from t_family where 1=1";
		  Object[]params={};
		  return super.queryOjects(sql, params);
		}catch(SQLException e){
			
			throw new DbException("查找所有家庭失败",e);
		}
	}
	
	public List<Family>findRencent()throws DbException{
		try{
		  String sql="select * from t_family where 1=1 order by creattime desc";
		  Object[]params={};
		  return super.queryOjects(sql, params);
		}catch(SQLException e){
			
			throw new DbException("查找最新的家庭编号失败",e);
		}
	}
	
	public List<Family> findListByFamilyholdername(String familyholdername)throws DbException{
		try{
			String sql="select * from t_family where familyholdername=? "; //不用管在Navicat里面的查询String类型要加''，这里sql语句不用，开始我写'?'一直报错
			Object[]params={familyholdername};
			return super.queryOjects(sql, params);
			}catch(SQLException e){
				
				throw new DbException("条件查找家庭失败",e);
			}
	}
	
	/**
	 * 分页
	 * @param sql
	 * @param params
	 * @param page
	 * @param size
	 * @return
	 * @throws SQLException
	 */
		public Page queryOjectsByPage(String sql,Object[]params,int pageNo,int size)throws SQLException{
			Connection conn =null;
			PreparedStatement pstmt=null;
			ResultSet rs=null;
			Page page=null;
			List<Family> datas=new ArrayList<Family>();
			try{
				int start=(pageNo-1)*size;
				conn=DbUtil.getConn();
				pstmt=conn.prepareStatement(sql+" limit "+start+","+size);
				int index=1;
				if(params!=null){
				  for(Object param:params){
					  if(param instanceof String){
						  pstmt.setString(index++, (String)param);
					  }
					  if(param instanceof Integer){
						  pstmt.setInt(index++, (Integer)param);
					  }
					  if(param instanceof Float){
						  pstmt.setFloat(index++, (Float)param);
					  }
				  }
				}
				rs=pstmt.executeQuery();
				while(rs.next()){
					Family family=handle(rs);
					datas.add(family);
				}
				//统计SQL对应记录数
				int total=count(sql, params);
				page=new Page(total, pageNo, size, datas);
				return page;
			}catch(SQLException e){
				e.printStackTrace();
				throw e;
			}
			
		}
}
